import React from 'react'
import _ from 'underscore'
import $ from 'jquery'
import dispatcher from 'lib/dispatcher'
import {Scope} from 'lib/scopeFactory'

import TimeStore from 'lib/stores/time-store'
import {coreApi,sortMap,parseToChart,parseToTopbar} from '../apis/core-util'
import tracelistApi from '../apis/tracelist'
export default class MainScope extends Scope {
  constructor(){
    super();
    this.$state = {
      chart_overview:{
        loading:true
      },
      chart_crash:{
        loading:true
      },
      bar_interaction:{
        loading:true
      },
      chart_session:{
        loading:true
      },
      chart_http:{
        loading:true
      },
      chart_error:{
        loading:true
      }
    }
    this.chart_overviewApi = coreApi({
      urlName:'interactionoverview',
      viewKey:'avg_exec_overview',
      limit:20
    },TimeStore.getValue);

    this.chart_crashApi = coreApi({
      urlName:BlueWare.platform.toLowerCase()+'_crash_overview',
      classify:'crashname',
      viewKey:'crash_count',
      customClassify:true,
      order:'301'
    },TimeStore.getValue);

    this.bar_interactionApi = coreApi({
      urlName:'interaction',
      classify:'measurementname',
      viewKey:'avg_exec',
      order:'202',
      limit:5,
      size:1
    },TimeStore.getValue);
    this.chart_sessionApi = coreApi({
      urlName:'session',
      classify:'appversion',
      viewKey:'session_count',
      order:1,
      limit:5
    },TimeStore.getValue)
    this.chart_httpApi = coreApi({
      urlName:'httptransaction',
      classify:'domain',
      classifyKeyField:'time',
      classifyCountField:'num',
      viewKey:'avg_resp',
      order:2,
      limit:5
    },TimeStore.getValue)
    this.chart_errorApi = coreApi({
      urlName:'errorrateoverview',
      viewKey:'error_rate',
      order:2
    },TimeStore.getValue)
  }
  fetch(){
    var _this = this
    this.chart_overviewApi.fetch().then(function(data,params){
      _this.$state.chart_overview = {
        data:parseToChart(data,params)
      }
    },function(err){
      _this.$state.chart_overview = {
        error:err
      }
    },function(params){
      _this.$state.chart_overview = {
        loading:true,
        params:params
      }
      _this.emit()
    }).always(function(){
      _this.emit()
    })

    this.chart_crashApi.fetch().then(function(data,params){
      _this.$state.chart_crash = {
        data:parseToChart(data.classifybyfixed,params)
      }
    },function(err){
      _this.$state.chart_crash = {
        error:err
      }
    },function(params){
      _this.$state.chart_crash = {
        loading:true,
        params:params
      }
      _this.emit()
    }).always(function(){
      _this.emit()
    })

    this.bar_interactionApi.fetch().then(function(data,params){
      _this.$state.bar_interaction = {
        data:parseToTopbar(data,params)
      }
    },function(err){
      _this.$state.bar_interaction = {
        error:err
      }
    },function(params){
      _this.$state.bar_interaction = {
        loading:true,
        params:params
      }
      _this.emit()
    }).always(function(){
      _this.emit()
    });
    this.chart_sessionApi.fetch().then(function(data,params){
      _this.$state.chart_session = {
        data:parseToChart(data,params)
      }
    },function(err){
      _this.$state.chart_session = {
        error:err
      }
    },function(params){
      _this.$state.chart_session = {
        loading:true,
        params:params
      }
      _this.emit()
    }).always(function(){
      _this.emit()
    });
    this.chart_httpApi.fetch().then(function(data,params){
      _this.$state.chart_http = {
        data:parseToChart(data,params)
      }
    },function(err){
      _this.$state.chart_http = {
        error:err
      }
    },function(params){
      _this.$state.chart_http = {
        loading:true,
        params:params
      }
      _this.emit()
    }).always(function(){
      _this.emit()
    });
    this.chart_errorApi.fetch().then(function(data,params){
      _this.$state.chart_error = {
        data:parseToChart(data,params)
      }
    },function(err){
      _this.$state.chart_error = {
        error:err
      }
    },function(params){
      _this.$state.chart_error = {
        loading:true,
        params:params
      }
      _this.emit()
    }).always(function(){
      _this.emit()
    });
  }
  $dismiss_version_notice(payload){
    var versionNotice = payload.versionNotice
    $.ajax({
      url:BlueWare.urlPrefix+'/app/'+BlueWare.appId+'/agentversion/ingoredAgentVersion.json',
      data:{
        'version':versionNotice.latestVersion,
        'type':versionNotice.type
      }
    })
  }
  $settime(payload){
    this.fetch()
  }
  $load(){
    this.fetch()
  }
}
